package com.crowntit.infrastructure.gateway;

import com.crowntit.types.enums.ResponseCode;
import com.crowntit.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class GroupBuyNotifyService {

    @Resource
    private OkHttpClient okHttpClient;

    public String groupBuyNotify(String notifyUrl, String notifyParamDTOJSON) throws Exception {
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody requestBody = RequestBody.create(mediaType, notifyParamDTOJSON);
            Request request = new Request.Builder()
                    .url(notifyUrl)
                    .post(requestBody)
                    .addHeader("content-type", "application/json")
                    .build();

            Response response = okHttpClient.newCall(request).execute();

            return response.body().string();
        } catch (Exception e) {
            log.error("拼团回调 HTTP 接口服务异常 {}", notifyUrl, e);
            throw new AppException(ResponseCode.HTTP_EXCEPTION.getCode(), ResponseCode.HTTP_EXCEPTION.getInfo());
        }
    }
}
